package com.luichi.ygj.presenter;

import android.os.Bundle;
import android.support.annotation.NonNull;

import com.luichi.data.DataModelFactory;
import com.luichi.data.base.IRequestId;
import com.luichi.domain.bean.User;
import com.luichi.domain.entity.LoginResult;
import com.luichi.data.base.BasePresenter;
import com.ruhaly.androidsdk.baseapi.ILoadDataView;

import rx.Observable;
import rx.functions.Action2;
import rx.functions.Func0;

/**
 * Created by hlr on 2015/11/11.
 */
public class LoginPresenter<T> extends BasePresenter<T> implements IRequestId {

    private User user;

    @Override
    protected void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        //这个里面所有的请求都需预定义在这里，然后通过start(REQUEST_LOGIN)方法调用
        requestLogin(REQUEST_LOGIN);
    }

    public void requestLogin(int request) {

        restartableFirst(request, new Func0<Observable<LoginResult>>() {
            @Override
            public Observable<LoginResult> call() {
                return DataModelFactory.getInstance().getLoginModel().requestLogin(user);
            }
        }, new Action2<T, LoginResult>() {
            @Override
            public void call(T view, LoginResult result) {
                ((ILoginView) view).notifyData(result);
            }
        }, new Action2<T, Throwable>() {
            @Override
            public void call(T view, Throwable throwable) {
                ((ILoadDataView) view).handleError(throwable);
            }
        });
    }


    public void requestLogin(String phoneNum, String pwd) {

        User user = new User();
        user.setPhone(phoneNum);
        user.setPwd(pwd);
        this.user = user;
        start(REQUEST_LOGIN);
    }

    @Override
    public void onSave(@NonNull Bundle state) {
        super.onSave(state);
    }
}
